package stdio.costa.imoca.api;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import stdio.costa.imoca.converter.Receivers2ReceiverList;
import stdio.costa.imoca.dto.ReceiverListDTO;
import stdio.costa.imoca.model.MocaUser;
import stdio.costa.imoca.model.Receiver;
import stdio.costa.imoca.spi.ReceiverSpi;

@Path("/receiver")
@Component
@Scope("request")
public class ReceiverApi extends BaseApi {

	@Autowired
	private ReceiverSpi receiverSpi;

	@Path("/add")
	@POST
	@Produces({ "application/json", "application/xml" })
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Transactional
	public Receiver add(@QueryParam("accessToken") String accessTokenCode,
			@QueryParam("province") String province,
			@QueryParam("city") String city,
			@QueryParam("district") String district,
			@QueryParam("detail") String detail, @QueryParam("name") String name) {

		MocaUser mocaUser = this.getMocaUser(accessTokenCode);

		Receiver receiver = null;

		// should add validation
		if (mocaUser != null
				&& this.receiverSpi.findReceiverByNameAndSender(name, mocaUser) == null) {
			receiver = new Receiver(mocaUser);
			receiver.setProvince(province);
			receiver.setCity(city);
			receiver.setDistrict(district);
			receiver.setName(name);
			receiver.setDetail(detail);
			receiverSpi.save(receiver);
		}

		return receiver;
	}

	@Path("/edit//{uuid}")
	@POST
	@Produces({ "application/json", "application/xml" })
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Transactional
	public Receiver edit(
			@PathParam("uuid") String uuid,
			@QueryParam("accessToken") String accessTokenCode,
			@QueryParam("province") String province,
			@QueryParam("city") String city,
			@QueryParam("district") String district,
			@QueryParam("detail") String detail, @QueryParam("name") String name) {

		MocaUser mocaUser = this.getMocaUser(accessTokenCode);

		Receiver receiver = null;

		// should add validation
		if (mocaUser != null
				&& (receiver = this.receiverSpi.findReceiverByUuidAndSender(
						uuid, mocaUser)) != null) {
			receiver.setProvince(province);
			receiver.setCity(city);
			receiver.setDistrict(district);
			receiver.setName(name);
			receiver.setDetail(detail);
			receiverSpi.update(receiver);
		}

		return receiver;
	}
	
	@Path("/add_weibo_receiver")
	@GET
	@Produces({ "application/json", "application/xml" })
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Transactional
	public Receiver addWeiboReceiver(@QueryParam("accessToken") String accessTokenCode,
			@QueryParam("weibo_uid") String weiboUid,
			@QueryParam("province") String province,
			@QueryParam("city") String city,
			@QueryParam("district") String district,
			@QueryParam("detail") String detail, @QueryParam("name") String name) {

		MocaUser mocaUser = this.getMocaUser(accessTokenCode);

		Receiver receiver = new Receiver(mocaUser);

		// should add validation
		if (mocaUser != null) {
			receiver.setProvince(province);
			receiver.setCity(city);
			receiver.setDistrict(district);
			receiver.setName(name);
			receiver.setWeiboUid(weiboUid);
			receiver.setDetail(detail);
			receiverSpi.save(receiver);
		}

		return receiver;
	}

	@Path("/delete/{uuid}")
	@GET
	@Produces({ "application/json", "application/xml" })
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Transactional
	public Receiver delete(
			@PathParam("uuid") String uuid,
			@QueryParam("accessToken") String accessTokenCode) {

		MocaUser mocaUser = this.getMocaUser(accessTokenCode);

		Receiver receiver = null;

		if ((receiver = this.receiverSpi.findReceiverByUuidAndSender(uuid,
				mocaUser)) != null) {
			this.receiverSpi.delete(receiver);
		}

		return receiver;
	}

	@Path("/get_all")
	@GET
	@Produces({ "application/json", "application/xml" })
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Transactional
	public ReceiverListDTO getAll(
			@QueryParam("accessToken") String accessTokenCode) {

		MocaUser mocaUser = this.getMocaUser(accessTokenCode);

		ReceiverListDTO receiverList = null;

		List<Receiver> receivers = null;

		if (mocaUser != null
				&& (receivers = this.receiverSpi
						.findAllReceiversBySender(mocaUser)) != null) {
			receiverList = Receivers2ReceiverList.convert(receivers);
		}

		return receiverList;
	}

	@Path("/{uuid}")
	@GET
	@Produces({ "application/json", "application/xml" })
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Transactional
	public Receiver get(@PathParam("uuid") String uuid,
			@QueryParam("accessToken") String accessTokenCode) {

		MocaUser mocaUser = this.getMocaUser(accessTokenCode);

		Receiver receiver = this.receiverSpi.findReceiverByUuidAndSender(uuid,
				mocaUser);

		return receiver;
	}
}
